<?php
namespace app\api\controller;
use app\api\controller\Cardusextend;
use app\api\controller\Pub;

use app\api\controller\Qrcodes;
use app\common\controller\ActionLogs;
use app\common\controller\Ekaipiao;
use app\common\controller\Platurl;
use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\EfapiaoGoods;
use app\common\model\EfapiaoList;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\WxOpen;
use Ekaipiao\Epiao;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;
use Ylb\Ylb;

class MachThree extends Pub
{

    //门店列表
    public function machlist($param)
    {
        try {
            $Merchant = new Merchant();
            $map['id'] = ['in', $param['merchantid']];
            $list = $Merchant->where($map)->field('id,merchant')->select();
            if ($list) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "SUCCESS";

                foreach ($list as $k => $v) {
                    $mach[$k]['merchantid'] = $v['id'];
                    $mach[$k]['merchantname'] = $v['merchant'];
                }
                $res_data['info'] = $mach;

            } else {
                $res_data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "没有找到该门店",);
            }
        } catch (\Exception $e) {
            $res_data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $res_data;
    }

    //门店详情
    public function getmachinfo($param)
    {
        try {
            $Merchant = new Merchant();
            $map['id'] = $param['merchantid'];
            $machinfo = $Merchant->where($map)->field('id,img,merchant,agent,top_mid,is_deposit')->find();
            if ($machinfo) {
                $Agent = new Agent();
                $agentinfo = $Agent->where(array('id' => $machinfo['agent']))->field('name,contacts,tel,open_url')->find();
                $CardMember = new CardMember();
                $cardid = $CardMember->where(['top_mid' => $machinfo['top_mid']])->value('card_id');
                $CardQrcode = new CardQrcode();
                $qrcode_url = $CardQrcode->where(['card_id' => $cardid, 'outer_str' => $param['merchantid']])->value('qrcode_url');
                $re_data['mch_img'] = $machinfo['img'];
                $re_data['mch_id'] = $machinfo['id'];
                $re_data['mch_name'] = $machinfo['merchant'];
                $re_data['mch_card_url'] = $qrcode_url;
                $re_data['mch_pay_url'] = "https://" . $agentinfo['open_url'] . "/qrcode/" . $machinfo['id'] . '.png';;
                $re_data['agent_name'] = $agentinfo['name'];
                $re_data['agent_contact'] = $agentinfo['contacts'];
                $re_data['agent_tel'] = $agentinfo['tel'];
                $re_data['is_deposit'] = $machinfo['is_deposit'];
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "SUCCESS";
                $re_param['info'] = $re_data;
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = "未查询到商户信息";
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = '系统错误';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '系统错误' . $e->getMessage();
        }

        return $re_param;

    }

    public function get_mach_qrcode($param){

        $Qrcodes = new Qrcodes();
        $CardQrcode = new CardQrcode();
        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $Agent = new Agent();
        $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid,id,agent')->find();
        $agentinfo  =$Agent->where(['id'=>$mach['agent']])->field('open_url,id')->find();
        if ($mach && $agentinfo) {
            $agentinfo['open_url'] = 'https://'.$agentinfo['open_url'];
            switch ($param['type']) {
                case "pay":
//                    $re_data['info']['qrcode'] = $Qrcodes->qrcode_mach($param['merchantid'], $agentinfo['open_url']);;
                    $re_data['info']['qrcode'] = $agentinfo['open_url'].'/index.php?s=/Index/pay/id/'.$param['merchantid'].'.html';
//                    $re_data['info']['qrcode_down'] = '/qrcode/' . $param['merchantid'] . '.png';
                    $re_data['return_code'] = "SUCCESS";
                    $re_data['return_msg'] = '成功';
                    break;
                case "cardus":
                    //查询是否创建了会员卡
                    $cardusinfo = $CardMember->where(['top_mid' =>$mach['top_mid'], 'is_del' => 1])->find();
                    if ($cardusinfo) {
                        //查看是否有此门店的二维码
                        $map['outer_str'] = $param['merchantid'];
                        $map['type'] = 1;
                        $map['url_mobile'] = ['neq',""];
                        $qrcode = $CardQrcode->where($map)->find();
//                        var_dump($CardQrcode);
//                        exit();
//                        $ActionLogs = new ActionLogs();
//                        $ActionLogs->writelog("osapi", "clientlog", "get_mach_qrcode", "1:", $qrcode);

                        if (!$qrcode) {
                            //未创建时查看此会员卡是否开启一键开卡
                            if ($cardusinfo['activate'] == 0) {

                                $re_data['return_code'] = "SUCCESS";
                                $re_data['return_msg'] = '您没有开启一键开卡，请开启后再点击查看';
                                $re_data['info']['qrcode'] = '/static/img/cardusbizonekey.png';
//                                $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                            } else {
                                //创建此门店的开卡码
                                $extdata['outer_str'] = $param['merchantid'];
                                $extdata['c_id'] = $cardusinfo['id'];
//                                $ActionLogs->writelog("osapi", "clientlog", "get_mach_qrcode", "2:", $extdata);
//                                return $extdata;
                                $Cardusextend = new Cardusextend();
                                $res = $Cardusextend->cardusbizadd($extdata);
//                                $ActionLogs->writelog("osapi", "clientlog", "get_mach_qrcode", "3:", $res);
//                                return $res;
                                if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'FAIL') {
                                    $re_data['return_code'] = "FAIL";
                                    $re_data['return_msg'] = $res['result_msg'];
                                }
                            }
                            $qrcode = $CardQrcode->where($map)->find();
                        }
                        //是否已有结果  无结果时创建带背景的二维码
                        if (!isset($re_data)) {
                            $re_data['info']['qrcode'] = $qrcode['url_mobile'];
//                            $re_data['info']['qrcode'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/bizcard/' . $qrcode['id'] . '.png';
//                            $re_data['info']['qrcode_down'] = '/bizcard/' . $qrcode['id'] . '.png';
                            $re_data['return_code'] = "SUCCESS";
                            $re_data['return_msg'] = '成功';
                        }
                    } else {
                        $re_data['return_code'] = "SUCCESS";
                        $re_data['return_msg'] = '未创建会员卡';
                        $re_data['info']['qrcode'] = '/static/img/notcardus.png';
                        $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                        $re_data['info']['bg_qrcode'] = '/static/img/notcardus_bg.png';
                        $re_data['info']['bg_qrcode_down'] = '/static/img/notshow.png';
                    }

                    break;
                default:
                    $re_data['info'] = '';
                    $re_data['return_code'] = "FAIL";
                    $re_data['return_msg'] = '失败';
                    break;
            }
        }else{
            $re_data['info'] = '';
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = '失败';
        }

        return $re_data;

    }

    //云喇叭
    public function set_mach_yunlaba($param)
    {

        try {
            $Merchant = new Merchant();
            $minfo = $Merchant
                ->alias('a')
                ->join('wn_agent b', 'a.agent=b.id', 'left')
                ->where(['a.id' => $param['merchantid']])
                ->field('a.ylb_deviceid as ylb_deviceid,a.ylb_vol as ylb_vol,b.ylb_app_cust_id as ylb_app_cust_id,b.ylb_app_cust_pwd as ylb_app_cust_pwd,b.ylb_username as ylb_username')
                ->find();
            $option = [
                'app_cust_id' => $minfo['ylb_app_cust_id'],
                'app_cust_pwd' => $minfo['ylb_app_cust_pwd'],
            ];

            $Ylb = new Ylb($option);
            $deviceid = $minfo['ylb_deviceid'];
            //END
            //绑定
            if ($param['type_method'] == "bind") {
                $res = $Ylb->bind($param['ylb_deviceid'], $minfo['ylb_username']);
//                $qwe['q'] = $param['ylb_deviceid'];
//                $qwe['w'] = $minfo['ylb_username'];
//                return $res;
                if ($res['code'] == 0) {
                    $map['id'] = $param['merchantid'];
                    $data['ylb_vol'] = !empty($param['ylb_vol']) ? $param['ylb_vol'] : "";
                    $data['ylb_deviceid'] = $param['ylb_deviceid'];
                    $add = $Merchant->save($data, $map);
                    if ($add) {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "SUCCESS";
                        $re_param['result_msg'] = '绑定成功';
                    } else {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "FAIL";
                        $re_param['result_msg'] = '绑定失败';
                    }

                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                    $re_param['result_code'] = "FAIL";
                    $re_param['result_msg'] = $res['code'].$res['msg'];
                }
            //解绑
            } elseif ($param['type_method'] == "unbind") {
                $res = $Ylb->unbind($deviceid, $minfo['ylb_username']);
                if ($res['code'] == 0) {
                    $map['id'] = $param['merchantid'];
                    $data['ylb_vol'] = "";
                    $data['ylb_deviceid'] = "";
                    $add = $Merchant->save($data, $map);
                    if ($add) {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "SUCCESS";
                        $re_param['result_msg'] = '解绑成功';
                    } else {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "FAIL";
                        $re_param['result_msg'] = '解绑失败';
                    }
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                    $re_param['result_code'] = "FAIL";
                    $re_param['result_msg'] = $res['code'].$res['msg'];
                }
            //音量设置
            } elseif ($param['type_method'] == "setvol") {
                $res = $Ylb->vol($deviceid, $param['ylb_vol']);
                if ($res['code'] == 0) {
                    $map['id'] = $param['merchantid'];
                    $map['ylb_deviceid'] = $param['ylb_deviceid'];
                    $data['ylb_vol'] = $param['ylb_vol'];
                    $add = $Merchant->save($data, $map);
                    if ($add) {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "SUCCESS";
                        $re_param['result_msg'] = '设置成功';
                    } else {
                        $re_param['return_code'] = "SUCCESS";
                        $re_param['return_msg'] = "SUCCESS";
                        $re_param['result_code'] = "FAIL";
                        $re_param['result_msg'] = '设置失败';
                    }
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                    $re_param['result_code'] = "FAIL";
                    $re_param['result_msg'] = $res['code'].$res['msg'];
                }
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = '系统错误1';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $re_param;
    }

    //云喇叭
    public function info_mach_yunlaba($param)
    {
        try {
            $Merchant = new Merchant();
            $info = $Merchant
                ->where(['id' => $param['merchantid']])
                ->field('ylb_deviceid,ylb_vol')
                ->find();
            if ($info){
                $info['bind_status'] = empty($info['ylb_deviceid'])?"N":"Y";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = '成功';
                $re_param['info'] = _unsetNull($info);
            }else{
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = '未获取到';
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = '系统错误';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $re_param;
    }

    public function fapiao($param)
    {
        $Merchant = new Merchant();
        if ($param['type'] == "send") {
            $s = $this->setfpinfo($param);
            if ($s['result_code'] == "SUCCESS") {
                $data['efp_address'] = $param['efp_address'];
                $data['efp_company'] = $param['efp_company'];
                $data['efp_contact'] = $param['efp_contact'];
                $data['efp_invoice'] = $param['efp_invoice'];
                $data['efp_isauto'] = isset($param['efp_isauto']) ? $param['efp_isauto'] : "N";
                $data['efp_receipt'] = isset($param['efp_receipt']) ? $param['efp_receipt'] : "N";
                $data['efp_kpjh'] = $param['efp_kpjh'];
                $data['efp_phone'] = $param['efp_phone'];
                $data['efp_s_pappid'] = $param['efp_s_pappid'];
                $data['efp_tel'] = $param['efp_tel'];
                $data['efp_time_out'] = $param['efp_time_out'];
                $up = $Merchant->save($data, ['id' => $param['merchantid']]);
                if ($up) {
                    $re_param['result_code'] = "SUCCESS";
                    $re_param['result_msg'] = "设置成功";
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                } else {
                    $re_param['result_code'] = "FAIL";
                    $re_param['result_msg'] = "设置失败";
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                }
            } else {
                $re_param = $s;
            }
        } elseif ($param['type'] == "getgoodsinfo") {
            $EfapiaoGoods = new EfapiaoGoods();
            $goodsinfo = $EfapiaoGoods->where('id', $param['id'])->find();
            $re_param['result_code'] = "SUCCESS";
            $re_param['result_msg'] = "获取成功";
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "SUCCESS";
            $re_param['goodsinfo'] = $goodsinfo;
        } elseif ($param['type'] == "goods") {
            $data['spmc'] = $param['spmc'];
            $data['spbm'] = $param['spbm'];
            $data['ggxh'] = $param['ggxh'];
            $data['slv'] = $param['slv'];
            $data['jldw'] = $param['jldw'];
            $data['spsl'] = $param['spsl'];
            $data['is_default'] = isset($param['is_default']) ? $param['is_default'] : "N";
            $data['hsbz'] = $param['hsbz'];
            $EfapiaoGoods = new EfapiaoGoods();
            if (!empty($param['goods_id'])) {
                $up = $EfapiaoGoods->save($data, ['id' => $param['goods_id']]);
            } else {
                $data['mid'] = $param['merchantid'];
                $up = $EfapiaoGoods->save($data);
            }
            if ($up) {
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "处理成功";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            } else {
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = "处理失败";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            }
        } elseif ($param['type'] == "delgoods") {
            $EfapiaoGoods = new EfapiaoGoods();
            $del = $EfapiaoGoods->where('id', $param['id'])->delete();
            if ($del) {
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "删除成功";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            } else {
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = "删除失败";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            }
        } elseif ($param['type'] == "getkaipiaolist") {
            $EfapiaoList = new EfapiaoList();
            $list = $EfapiaoList->where(['mid' => $param['merchantid']])
                ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])->order('status asc')->select();
            $re_param["total"] =$EfapiaoList->where(['mid' => $param['merchantid']])->count('id');
            //获取的记录
            $re_param["rows"] = $list;
//                return json($re_param);
        } elseif ($param['type'] == "getfpgoodsinfo") {
            $EfapiaoList = new EfapiaoList();
            $list = $EfapiaoList->where(['id' => $param['id']])->value('goodsinfo');
            $list = json_decode($list, true);
            $i = 0;
            foreach ($list as $k => $v) {
                $lists[$i]['SYYHZCBZ'] = isset($list[$k]['SYYHZCBZ']) ? $list[$k]['SYYHZCBZ'] : "";
                $lists[$i]['YHZCSM'] = isset($list[$k]['YHZCSM']) ? $list[$k]['YHZCSM'] : "";
                $lists[$i]['ggxh'] = isset($list[$k]['ggxh']) ? $list[$k]['ggxh'] : "";
                $hsbz = isset($list[$k]['hsbz']) ? $list[$k]['hsbz'] : "0";
                $lists[$i]['hsbz'] = (isset($list[$k]['hsbz']) ? $list[$k]['hsbz'] : "0") == "0" ? "不含税" : "含税";
                $lists[$i]['je'] = isset($list[$k]['je']) ? $list[$k]['je'] : "";
                $lists[$i]['jldw'] = isset($list[$k]['jldw']) ? $list[$k]['jldw'] : "";
                $lists[$i]['se'] = isset($list[$k]['se']) ? $list[$k]['se'] : "";
                $lists[$i]['slv'] = isset($list[$k]['slv']) ? $list[$k]['slv'] : "";
                $lists[$i]['spdj'] = isset($list[$k]['spdj']) ? $list[$k]['spdj'] : "";
                $lists[$i]['spmc'] = isset($list[$k]['spmc']) ? $list[$k]['spmc'] : "";
                $lists[$i]['spsl'] = isset($list[$k]['spsl']) ? $list[$k]['spsl'] : "";
                $lists[$i]['spbm'] = isset($list[$k]['spbm']) ? $list[$k]['spbm'] : "";
                $i++;
            }
            $re_param = $lists;
//                return json($re_param);
        } elseif ($param['type'] == "mtfapiao") {
            $mapmt['id'] = $param['id'];
            $EfapiaoList = new EfapiaoList();
            $Ekaipiao = new Ekaipiao();
            $orderid = $EfapiaoList->where($mapmt)->value('orderid');
            $lists = $Ekaipiao->send_kp($orderid);
            $re_param = $lists;
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "SUCCESS";
            $re_param['result_code'] = "FAIL";
            $re_param['result_msg'] = "错误的方式";
        }
        return json($re_param);
    }

    private function setfpinfo($param)
    {
        $map['id'] = $param['merchantid'];
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $user = $Merchant->where($map)->find();
        $config = $Agent->where(array('id' => $user['agent']))->find();
        $users = $WxOpen->where(array('id' => $config['wx_open_id']))->find();
        $usersopen = $WxOpen->where(array('id' => $user['wx_open_id']))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $usersopen['authorizer_appid'],
            'authorizer_refresh_token' => $usersopen['authorizer_refresh_token'],
            // 'partnerkey'=>$agent['partnerkey']
        );
        $weObj = new Wxobjopen($options);
        $set1_data['contact'] = array(
            "time_out" => $param['efp_time_out'],
            "phone" => $param['efp_phone'],
        );
        $setup1 = $weObj->setcontactinfo($set1_data);
        if ($setup1['errcode'] == 0) {
            $setup2_data['paymch_info'] = array(
                'mchid' => $user['sub_mch_id'],
                's_pappid' => $param['efp_s_pappid'],
            );
            $setup2 = $weObj->setpaymch($setup2_data);
//            return $setup2_data;
            if ($setup2['errcode'] == 0) {
                $setup3 = $this->efapiaoset($param['merchantid']);
                if ($setup3['result_code'] == "SUCCESS") {
                    $re_param['result_code'] = "SUCCESS";
                    $re_param['result_msg'] = "SUCCESS";
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "SUCCESS";
                } else {
                    $re_param = $setup3;
                }
            } else {
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = $setup2['errmsg'];
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            }
        } else {
            $re_param['result_code'] = "FAIL";
            $re_param['result_msg'] = $setup1['errmsg'];
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "SUCCESS";
        }
        return $re_param;
    }

    private function efapiaoset($mid)
    {
        $Merchant = new Merchant();
        $machinfo = $Merchant->where('id', $mid)->find();
        if ($machinfo['efp_token'] == "") {
            $Epiao = new Epiao();
            $data = array(
                "company" => $machinfo['efp_company'],
                "invoice" => $machinfo['efp_invoice'],
                "kpjh" => $machinfo['efp_kpjh'],
                "address" => $machinfo['efp_address'],
                "tel" => $machinfo['efp_tel'],
                "contact" => $machinfo['efp_contact'],
            );
            $at = $Epiao->register($data);
            if ($at && $at['errcode'] == "200") {
                $token = $at['info'][0]['token'];
                $Merchant->save(['efp_token' => $token, 'efp_time' => time()], ['id' => $mid]);
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "设置成功";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            } else {
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "开票平台注册失败";
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
            }
        } else {
            $re_param['result_code'] = "SUCCESS";
            $re_param['result_msg'] = "设置成功";
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "SUCCESS";
        }
        return $re_param;
    }


    //交易记录

    public function paylog_extend($param)
    {
        try {
            $Pay = new Paylog();
            $page = ($param['page'] - 1) * 10;
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $map1 = [];
//            $map['a.pay_statu'] = array('in', '1,2,3');
            $map['a.mid'] = $param['merchantid'];
            $map['a.pay_cate'] = array('neq', 'recharge');
            $map['a.pay_statu'] = array(array('neq', 0), array('neq', 8), 'and');
            if ($start_time == "" && $end_time == "") {
                $map['a.add_time'] = array(array('egt', 0), array('elt', time()), 'and');
            } elseif ($start_time !== "" && $end_time == "") {
                $map['a.add_time'] = array(array('egt', $start_time), array('elt', time()), 'and');
            } elseif ($start_time == "" && $end_time !== "") {
                $map['a.add_time'] = array(array('egt', 0), array('elt', $end_time), 'and');
            } else {
                $map['a.add_time'] = array(array('egt', $start_time), array('elt', $end_time), 'and');
            }
            //END
            //2019-10-12 加入拓展条件
            //支付状态
            if (!empty($param['pay_statu'])){
                $map['a.pay_statu'] = $param['pay_statu'];
            }
            //款台账号
            if (!empty($param['d_cashier'])){
                $map['a.d_cashier'] = array('like', '%' . trim($param['d_cashier']) . '%');
            }
            //支付方式
            if (!empty($param['pay_fs'])){
                $map['a.pay_fs'] = $param['pay_fs'];
            }else{
                $map1['a.pay_fs'] = array('in', [0, 1, 2, 3, 4, 5, 8]);
            }
            //END


            //END
            $paylog = $Pay->alias('a')
                ->join('wn_merchant b', 'b.id=a.mid')
                ->where($map)
                ->where($map1)
                ->field('b.merchant as merchantname,a.orderid as out_trade_no,a.price as total_fee,a.add_time as end_time,a.pay_fs as channel,a.tk_price as refund_fee,a.service_charge,cashier,d_cashier')
                ->order('a.add_time desc')
                ->limit($page, 10)->select();
//            return $paylog;
            $total = $Pay->alias('a') ->where($map)->where($map1)->count();
            $real_fee = $Pay->alias('a') ->where($map)->where($map1)->sum('price');
            $yf_fee = $Pay->alias('a') ->where($map)->where($map1)->sum('yf_price');
//
//            $total = 0;
//            $real_fee = 0;
//            $yf_fee = 0;
            if ($paylog) {
                foreach ($paylog as $k => $v) {
                    $paylog[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
                    $paylog[$k]['deivce_id'] = !empty($v['cashier']) ? $v['cashier'] : $v['d_cashier'];
                    unset($paylog[$k]['cashier']);
                    unset($paylog[$k]['d_cashier']);
                }
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = "";
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = "获取成功";
                $rarr['real_fee'] = $real_fee;
                $rarr['yf_fee'] = $yf_fee;
                $rarr['total'] = $total;
                $rarr['info'] = $paylog;
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = "";
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = "没有查询到相关记录";
            }

        } catch (\Exception $e) {

            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = "系统错误" . $e->getMessage();
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = "系统错误" . $e->getMessage();
            $rarr['123123'] = "qweqweqwe";
        }
        return $rarr;
    }

    //支付状态
    public function get_payterms_list($param){
        try {
            $data['pay_statu'] = [
                ['name'=>'未支付','value'=>'0'],
                ['name'=>'已支付','value'=>'1'],
                ['name'=>'已退款','value'=>'2'],
                ['name'=>'有退款','value'=>'3'],
                ['name'=>'押金已消费','value'=>'6'],
//                ['name'=>'订单已关闭','value'=>'8']
            ];
            $data['pay_fs'] = [
                ['name'=>'微信支付','value'=>'0'],
                ['name'=>'支付宝支付','value'=>'1'],
                ['name'=>'微信刷卡支付','value'=>'2'],
                ['name'=>'支付宝刷卡支付','value'=>'3'],
                ['name'=>'现金支付','value'=>'4'],
                ['name'=>'会员卡支付','value'=>'8'],
//                ['name'=>'押金','value'=>'20'],
            ];
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = "";
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = "获取成功";
            $rarr['info'] = $data;
        } catch (\Exception $e) {

            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = "系统错误" . $e->getMessage();
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = "系统错误" . $e->getMessage();
        }
        return $rarr;
    }

    //门店商户号
    public function get_mach_submchid($param){
        try {
            $Merchant = new Merchant();

            $map['id'] = $param['merchantid'];
            $machinfo = $Merchant->where($map)->field('id,sub_mch_id,sub_appid,app_appid,app_mch_id')->find();
            if ($machinfo) {
                $re_data['merchantid'] = $machinfo['id'];
                $re_data['sub_mch_id'] = $machinfo['sub_mch_id'];
                $re_data['sub_appid'] = $machinfo['sub_appid'];
                $re_data['app_appid'] = $machinfo['app_appid'];
                $re_data['app_mch_id'] = $machinfo['app_mch_id'];
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "SUCCESS";
                $re_param['return_code'] = "SUCCESS";
                $re_param['info'] = _unsetNull($re_data);
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = "未查询到门店信息";
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = '系统错误';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $re_param;
    }

}
